package com.css.fxfzysx.modules.ysxManagement.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.modules.vabStructure.service.SrcCommonService;
import com.css.fxfzysx.modules.ysxManagement.entity.*;
import com.css.fxfzysx.modules.ysxManagement.repository.RcStructureRepository;
import com.css.fxfzysx.modules.ysxManagement.repository.ZqtCiRepository;
import com.css.fxfzysx.modules.ysxManagement.service.RcStructureService;
import com.css.fxfzysx.modules.ysxManagement.service.ZqtCiService;
import com.css.fxfzysx.modules.ysxManagement.vo.CStructureCiVo;
import com.css.fxfzysx.modules.ysxManagement.vo.CsValueVo;
import com.css.fxfzysx.modules.ysxManagement.vo.MsCoefficientVo;
import com.css.fxfzysx.util.PlatformDateUtils;
import com.css.fxfzysx.util.PlatformObjectUtils;
import com.css.fxfzysx.util.PlatformSessionUtils;
import com.css.fxfzysx.util.UUIDGenerator;
import com.css.fxfzysx.web.PlatformSessionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class RcStructureServiceImpl implements RcStructureService {

    @Autowired
    private RcStructureRepository rcStructureRepository;

    @Autowired
    ZqtCiRepository zqtCiRepository;

    @Autowired
    private ZqtCiService zqtCiService;

    @Autowired
    private SrcCommonService srcCommonService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse saverOrUpdateCStructureCi(CStructureCiVo cStructureCiVo) {
        try {
            List<CStructureCiEntity> cStructureCiEntityList = cStructureCiVo.getStructureCiList();
            List<DynamicFieldsEntity> dynamicFieldsEntityList = cStructureCiVo.getDynamicFieldsEntityList();
            if (PlatformObjectUtils.isNotEmpty(cStructureCiEntityList)) {
                cStructureCiEntityList.forEach(it -> {
                    if (PlatformObjectUtils.isEmpty(it.getCcsrId())) {
                        it.setCcsrId(UUIDGenerator.getUUID());
                        it.setCreateUser(PlatformSessionContext.getUserID());
                        it.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
                        it.setDelFlag("0");
                        it.setDefaultData("1");
                        rcStructureRepository.saverCStructureCi(it);
                    } else {
                        if (!it.equals(rcStructureRepository.getCstructureCiById(it.getCcsrId()))) {
                            it.setUpdateUser(PlatformSessionUtils.getUserId());
                            it.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
                            rcStructureRepository.updateCStructureCi(it);
                            srcCommonService.updateEntirety(it.getCcsrId(), "ci");
                        }
                    }
                });
            }
            zqtCiService.saveOrUpdateDynamicFields(dynamicFieldsEntityList);
            return RestResponse.succeed("成功!");
        } catch (Exception e) {
            String errorMessage = "失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse getAllCStructureCi(String titleName) {
        try {
            CStructureCiVo cStructureCiVo = new CStructureCiVo();
            List<CStructureCiEntity> cStructureCiList = rcStructureRepository.getCStructureCi();
            List<DynamicFieldsEntity> dynamicFields = zqtCiRepository.getDynamicFields(titleName);
            cStructureCiVo.setStructureCiList(cStructureCiList);
            cStructureCiVo.setDynamicFieldsEntityList(dynamicFields);
            return RestResponse.succeed(cStructureCiVo);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse deleteCStructureCiById(String cStructureCiIds, String arrangeIds) {
        try {
            if(PlatformObjectUtils.isNotEmpty(cStructureCiIds)){
                for (String cStructureCiId : cStructureCiIds.split(",")) {
                    rcStructureRepository.deleteCStructureCiById(cStructureCiId);
                }
            }
            if(PlatformObjectUtils.isNotEmpty(arrangeIds)){
                for (String arrangeId : arrangeIds.split(",")) {
                    zqtCiRepository.deleteArrangeById(arrangeId);
                    rcStructureRepository.deleteCStructureCiByProjectId(arrangeId);
                }
            }
            return RestResponse.succeed("删除成功!");
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse saverOrUpdateMsCoefficient(MsCoefficientVo msCoefficientVo) {
        try {
            List<MsCoefficientEntity> msCoefficientEntityList = msCoefficientVo.getMsCoefficientEntityList();
            List<DynamicFieldsEntity> dynamicFieldsEntityList = msCoefficientVo.getDynamicFieldsEntityList();
            if (PlatformObjectUtils.isNotEmpty(msCoefficientEntityList)) {
                msCoefficientEntityList.forEach(it -> {
                    if (PlatformObjectUtils.isEmpty(it.getCcprId())) {
                        it.setCcprId(UUIDGenerator.getUUID());
                        it.setCreateUser(PlatformSessionContext.getUserID());
                        it.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
                        it.setDelFlag("0");
                        it.setDefaultData("1");
                        rcStructureRepository.saverMsCoefficient(it);
                    } else {
                        MsCoefficientEntity msCoefficientEntity = rcStructureRepository.getMsCoefficientById(it.getCcprId());
                        if (!it.equals(msCoefficientEntity)) {
                            it.setUpdateUser(PlatformSessionUtils.getUserId());
                            it.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
                            rcStructureRepository.updateMsCoefficient(it);
                            //异步调用更新业务数据方法
                            srcCommonService.updateEntirety(it.getCcprId(), "ms");
                        }
                    }
                });
            }
            zqtCiService.saveOrUpdateDynamicFields(dynamicFieldsEntityList);
            return RestResponse.succeed("成功!");
        } catch (Exception e) {
            String errorMessage = "失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse getAllMsCoefficient(String titleName) {
        try {
            MsCoefficientVo msCoefficientVo = new MsCoefficientVo();
            List<MsCoefficientEntity> msCoefficientList = rcStructureRepository.getMsCoefficient();
            List<DynamicFieldsEntity> dynamicFields = zqtCiRepository.getDynamicFields(titleName);
            msCoefficientVo.setMsCoefficientEntityList(msCoefficientList);
            msCoefficientVo.setDynamicFieldsEntityList(dynamicFields);
            return RestResponse.succeed(msCoefficientVo);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse deleteMsCoefficientById(String msCoefficientIds, String arrangeIds) {
        try {
            if(PlatformObjectUtils.isNotEmpty(msCoefficientIds)){
                for (String msCoefficientId : msCoefficientIds.split(",")) {
                    rcStructureRepository.deleteMsCoefficientById(msCoefficientId);
                }
            }
            if(PlatformObjectUtils.isNotEmpty(arrangeIds)){
                for (String arrangeId : arrangeIds.split(",")) {
                    zqtCiRepository.deleteArrangeById(arrangeId);
                    rcStructureRepository.deleteMsCoefficientByProjectId(arrangeId);
                }
            }
            return RestResponse.succeed("删除成功!");
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse saveOrUpdateCsValue(CsValueVo csValueVo) {
        try {
            List<CsValueEntity> csValueList = csValueVo.getCsValueList();
            List<DynamicFieldsEntity> dynamicFieldsList = csValueVo.getDynamicFieldsList();
            if (PlatformObjectUtils.isNotEmpty(csValueList)) {
                csValueList.forEach(it -> {
                    if (PlatformObjectUtils.isEmpty(it.getCcsvId())) {
                        it.setCcsvId(UUIDGenerator.getUUID());
                        it.setCreateUser(PlatformSessionContext.getUserID());
                        it.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
                        it.setDelFlag("0");
                        it.setDefaultData("1");
                        rcStructureRepository.saveCsValue(it);
                    } else {
                        if (!it.equals(rcStructureRepository.getCsValueById(it.getCcsvId()))) {
                            it.setUpdateUser(PlatformSessionUtils.getUserId());
                            it.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
                            rcStructureRepository.updateCsValue(it);
                            srcCommonService.updateEntirety(it.getCcsvId(), "fv");
                        }
                    }
                });
            }
            zqtCiService.saveOrUpdateDynamicFields(dynamicFieldsList);
            return RestResponse.succeed("成功!");
        } catch (Exception e) {
            String errorMessage = "失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse getAllCsValue(String titleName) {
        try {
            CsValueVo csValueVo = new CsValueVo();
            List<CsValueEntity> csValueList = rcStructureRepository.getAllCsValue(titleName);
            List<DynamicFieldsEntity> dynamicFields = zqtCiRepository.getDynamicFields(titleName);
            csValueVo.setCsValueList(csValueList);
            csValueVo.setDynamicFieldsList(dynamicFields);
            return RestResponse.succeed(csValueVo);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse deleteCsValueById(String csValueIds, String arrangeIds) {
        try {
            if(PlatformObjectUtils.isNotEmpty(csValueIds)){
                for (String csValueId : csValueIds.split(",")) {
                    rcStructureRepository.deleteCsValueById(csValueId);
                }
            }
            if(PlatformObjectUtils.isNotEmpty(arrangeIds)){
                for (String arrangeId : arrangeIds.split(",")) {
                    zqtCiRepository.deleteArrangeById(arrangeId);
                    rcStructureRepository.deleteCsValueByProjectId(arrangeId);
                }
            }
            return RestResponse.succeed("删除成功!");
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }
}
